<template>
  <van-popup v-model:show="showPopup" position="center" round closeable>
    <div class="wrapper">
      <div class="title">修改名称</div>
      <div class="form-box">
        <div class="form-item">
          <div class="form-item-title">志愿表名称：</div>
          <div class="form-item-content">
            <van-field
              ref="scoreInputRef"
              class="form-item-content-item"
              v-model="volunteerName"
              :required="true"
              :border="true"
              :autofocus="true"
              placeholder="请输入志愿表名称"
            />
          </div>
        </div>
      </div>
      <div class="bottom-btn">
        <div class="left-btn" @click="handleCancel">取 消</div>
        <div class="right-btn" @click="handleConfirm">确 定</div>
      </div>
    </div>
  </van-popup>
</template>

<script setup>
import { ref, reactive, onMounted, nextTick, watch } from 'vue';

const props = defineProps({
  showPopup: {
    type: Boolean,
    default: false,
  },
  name: {
    type: String,
    default: '',
  },
});

const showPopup = defineModel('showPopup');
const volunteerName = ref('');

const emit = defineEmits(['handleCancel', 'handleConfirm']);

const handleCancel = () => {
  emit('update:showPopup', false);
};

const handleConfirm = () => {
  emit('handleConfirm', volunteerName.value.replace(/[\s\/\\]+/g, '-'));
  emit('update:showPopup', false);
};

const scoreInputRef = ref(null);

watch(
  () => props.name,
  (newVal) => {
    volunteerName.value = newVal;
  }
);

watch(
  () => props.showPopup,
  (newVal) => {
    if (newVal) {
      nextTick(() => {
        scoreInputRef.value.focus();
      });
    }
  }
);
</script>

<style scoped lang="scss">
.wrapper {
  .title {
    font-size: 28px;
    color: $title-color;
    font-weight: bold;
    padding: 24px 32px;
    border-bottom: 1px solid $border-color;
  }
  .form-box {
    height: 200px;
    padding: 0 32px;
    box-sizing: border-box;
    position: relative;
    display: flex;
    justify-content: center;
    align-items: center;
    .form-item {
      position: relative;
      z-index: 1;
      display: flex;
      align-items: center;
      justify-content: space-between;
      padding: 24px 0;
      font-weight: bold;
      .form-item-title {
        display: flex;
        align-items: center;
        font-size: 24px;
        color: $title-color;
        font-weight: bold;
        line-height: 33px;
      }
      .form-item-content {
        display: flex;
        align-items: center;
        font-size: 24px;
        color: $title-color;
        .form-item-content-item {
          margin-right: 12px;
        }
        .arrow {
          width: 24px;
          height: 24px;
        }
        :deep(.van-field) {
          background-color: #f6fbfe;
          .van-field__control {
            font-size: 24px;
            color: #000000;
          }
        }
      }
    }
    .tips {
      margin-top: 40px;
      font-size: 20px;
      color: $title-color;
      .tips-item {
        width: 594px;
        font-weight: 400;
        font-size: 18px;
        color: #888888;
        line-height: 25px;
        &:first-child {
          margin-bottom: 18px;
        }
      }
    }
    .btn {
      width: 100%;
      height: 64px;
      line-height: 64px;
      text-align: center;
      background: $primary-color;
      border-radius: 12px;
      font-weight: bold;
      font-size: 24px;
      color: #ffffff;
      margin-top: 54px;
    }
  }
  .bottom-btn {
    padding: 30px;
    background: #ffffff;
    box-shadow: 0px -2px 4px 0px rgba(188, 188, 188, 0.5);
    box-sizing: border-box;
    display: flex;
    flex-direction: row;
    justify-content: center;
    gap: 24px;
    z-index: 2;

    .left-btn,
    .right-btn {
      flex-shrink: 0;
      width: 48%;
      height: 72px;
      background: #f8f8f8;
      border-radius: 6px;
      font-family: PingFang-SC, PingFang-SC;
      font-weight: bold;
      font-size: 24px;
      color: #999999;
      display: flex;
      flex-direction: row;
      align-items: center;
      justify-content: center;
    }

    .left-btn {
      background: #f8f8f8;
      border: 1px solid #f8f8f8;
      &.active {
        border: 1px solid $primary-color;
        color: $primary-color;
      }
    }
    .right-btn {
      color: #ffffff;
      background: $primary-color;
    }
  }
}
</style>
